package com.scs.application.modules.fsd.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scs.application.core.annotation.PassToken;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.fsd.dto.LoginParam;
import com.scs.application.modules.fsd.dto.SaveStockDTO;
import com.scs.application.modules.fsd.entity.FsdStation;
import com.scs.application.modules.fsd.service.FsdUserService;
import com.scs.application.modules.fsd.service.IntelliAPIService;
import com.scs.application.modules.fsd.vo.LoginResult;
import com.scs.application.modules.fsd.vo.SPDResponseResult;
import com.scs.application.modules.sys.model.RestResult;
import com.scs.application.modules.volant.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @Description: 智能柜业务接口
 */
@RestController
@RequestMapping("/intelli")
public class IntelliAPIController {

    @Autowired
    private FsdUserService userService;
    @Autowired
    private IntelliAPIService intelliAPIService;

    @PassToken
    @PostMapping(value = "/user/login")
    public RestResult login(@RequestBody @Valid LoginParam loginParam, @RequestHeader("Intellic-configuration") String config) {
        Map<String,String> token = new HashMap<>();
        token.put("token",userService.login(loginParam.getJobNumber(), loginParam.getPassword(),JSONObject.parseObject(config).getString("stationCode")));
        return RestResult.ok("登录成功", token);
    }

    @PassToken
    @PostMapping(value = "/user/finger/login")
    public RestResult fingerLogin(@RequestBody Map<String, String> map, @RequestHeader("Intellic-configuration") String config) {
        Map<String,String> token = new HashMap<>();
        token.put("token",userService.fingerLogin(map.get("id"),JSONObject.parseObject(config).getString("stationCode")));
        return RestResult.ok("登录成功", token);
    }
    @PassToken
    @PostMapping(value = "/user/card/login")
    public RestResult cardLogin(@RequestBody Map<String, String> map, @RequestHeader("Intellic-configuration") String config) {
        Map<String,String> token = new HashMap<>();
        token.put("token",userService.cardLogin(map.get("id"),JSONObject.parseObject(config).getString("stationCode")));
        return RestResult.ok("登录成功", token);
    }

    @DeleteMapping(value = "/user/logout")
    public RestResult logout( @RequestHeader("Intellic-configuration") String config) {
        userService.logout(JSONObject.parseObject(config).getString("stationCode"));
        return RestResult.ok("退出登录成功", "success");
    }

    @GetMapping(value = "/user/info")
    public RestResult getUserInfoByToken(@RequestParam("token") String token) {
        LoginResult loginResult = intelliAPIService.getUserInfoByToken(token);
        return RestResult.ok("获取该用户所拥有的权限成功", loginResult);
    }

    @GetMapping(value = "/mouldFive/getVideoListData")
    public RestResult getUserAuthLogByStationCode(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("获取站点登录记录成功", userService.getUserAuthLogByStationCode(JSONObject.parseObject(config).getString("stationCode")));
    }
    /**
     * 查询所有站点
     *
     * @return
     */
    @GetMapping(value = "/config/getStationCode")
    public RestResult getStationCode() {
        return RestResult.ok("获取站点成功", intelliAPIService.getStationCode());
    }

    @PostMapping(value = "/config/submitStationCode")
    public RestResult submitStationCode(@RequestBody Map<String, String> map) {
        return RestResult.ok("站点绑定成功", intelliAPIService.submitStationCode(map.get("stationCode")));
    }

    @PostMapping(value = "/config/changePwd")
    public RestResult changePwd(@RequestBody Map<String, String> map) {
        userService.changePwd(map.get("userId"), map.get("newPwd"));
        return RestResult.ok("密码修改成功");
    }

    @GetMapping(value = "/config/userValidate")
    public RestResult getUserValidate(@RequestParam("jobNumber") String jobNumber,@RequestParam("password") String password) {
        Map<String, Object> map = intelliAPIService.getUserValidate(jobNumber, password);
        if (map.size() > 0)
            return RestResult.ok("用户验证通过", map);
        else return RestResult.error("工号或密码错误");
    }

    /**
     * 同步该站点的指纹
     *
     * @return
     */
    @GetMapping(value = "/config/syncFinger")
    public RestResult SyncFinger(@RequestHeader("Intellic-configuration") String config) {
        List<Map<String,Object>> list =  intelliAPIService.SyncFinger(JSONObject.parseObject(config).getString("stationCode"));
        if(list.size()==0) return RestResult.error("该站点下无用户");
        return RestResult.ok("获取站点成功",list);
    }

    @GetMapping(value = "/config/getStationPara")
    public RestResult getStationPara(@RequestParam("stationCode") String stationCode) {
        FsdStation station = intelliAPIService.getStationPara(stationCode);
        if(station==null) return  RestResult.error("站点编号错误");
        return RestResult.ok("获取站点配置成功",station);
    }

    @GetMapping(value = "/config/getLastScanData")
    public RestResult getLastScanData(@RequestHeader("Intellic-configuration") String config) {
        List<Map<String,String>>  lastScanData = intelliAPIService.getLastScanData(JSONObject.parseObject(config).getString("stationCode"));
        return RestResult.ok("获取上次库存异动成功",lastScanData);
    }

    @PostMapping(value = "/config/submitFingerTemplet")
    public RestResult submitFingerTemplet(@RequestBody Map<String, String> map) {
        if (intelliAPIService.submitFingerTemplet(map.get("id"), map.get("templet")))
            return RestResult.ok("指纹录入成功");
        else
            return RestResult.error("指纹录入失败");
    }
    @PostMapping(value = "/config/netTest")
    public RestResult netTest() {
        return RestResult.ok("网络连接正常");
    }

    @GetMapping(value = "/config/timeService")
    public RestResult getTimeService() {
        Map<String,String> time = new HashMap<>();
        time.put("serverTime", DateUtils.format(new Date(), DateUtil.datetimeFormat));
        return RestResult.ok("获取服务器当前时间成功", time);
    }

    @GetMapping(value = "/mainView/getShortcutData")
    public RestResult getShortcutData(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("获取统计信息成功", intelliAPIService.getShortcutData(JSONObject.parseObject(config).getString("stationCode")));
    }

    @GetMapping(value = "/mainView/getMessageData")
    public RestResult getMessageData(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("获取提示信息成功", intelliAPIService.getMessageData(JSONObject.parseObject(config).getString("stationCode")));
    }

    @GetMapping(value = {"/mouldOne/getMouldOne","/mouldTwo/getMouldTwo","/mouldThree/getMouldThree"})
    public RestResult getMouldOne(@RequestParam("business") String business) {
        return RestResult.ok("获取模板信息成功", intelliAPIService.getMouldOne(business));
    }

    @GetMapping(value = "/mouldTwo/getListTwo")
    public RestResult getListTwo(@RequestParam("business") String business) {
        return RestResult.ok("获取模板信息成功", intelliAPIService.getListTwo(business));
    }

    /**
     * 获取库存统计信息
     * @return
     */
    @GetMapping(value = "/mouldThree/getData")
    public RestResult getStockData(@RequestHeader("Intellic-configuration") String config) {
        return RestResult.ok("库存查询成功", intelliAPIService.getStockData(JSONObject.parseObject(config).getString("stationCode")));
    }

    @PostMapping(value = "/common/scan")
    public RestResult getScanResult(@RequestHeader("Intellic-configuration") String config, @RequestBody Map<String, String> map) {
        List<SaveStockDTO> stockList = JSONArray.parseArray(map.get("data"), SaveStockDTO.class);
        if(stockList==null) return RestResult.error("未扫描到标签!");
        return RestResult.ok("扫描成功", intelliAPIService.getScanResult(JSONObject.parseObject(config).getString("stationCode"), map.get("business"), stockList));
    }

    @PostMapping(value = {"/mouldOne/submit","/mouldTwo/submit"})
    public RestResult submit(@RequestHeader("Intellic-configuration") String config, @RequestBody Map<String, String> map) {
        String rfids = JSONObject.parseObject(map.get("data")).getString("difMat");
        List<String> rfidList = new ArrayList<>(Arrays.asList((StringUtils.strip(rfids, "[]").replace("\"","").split(","))));
        //备货提交
        String preparation = "";
        //手术备货
        if (map.get("business").equals("Operation_Preparation"))
            preparation = JSONObject.parseArray(map.get("listTwoData")).getJSONObject(0).getJSONObject("opNo").getString("value");
        //手术间备货
        if (map.get("business").equals("OperatingRoom_Preparation"))
            preparation = JSONObject.parseArray(map.get("listTwoData")).getJSONObject(0).getJSONObject("opRoomCode").getString("value");
        System.out.println(preparation);

        SPDResponseResult responseResult = intelliAPIService.submit(JSONObject.parseObject(config).getString("stationCode"), map.get("business"), rfidList,preparation);
        if (responseResult.getCode() == 0)
            return RestResult.ok("操作成功");
        return RestResult.error("操作失败", responseResult.getMsg());
    }

    /**
     * 获取报表数据
     * @return
     */
    @GetMapping(value = "/mouldFour/getSheetData")
    public RestResult getSheetData(@RequestHeader("Intellic-configuration") String config,@RequestParam("sheetTab") String sheetTab) {
        return RestResult.ok("报表查询成功", intelliAPIService.getSheetData(JSONObject.parseObject(config).getString("stationCode"),sheetTab));
    }
}
